package path;

import geom.PolarVector;
import geom.Vector;
import geom.VectorSpace;

import java.util.ArrayList;


import processing.core.PApplet;
import processing.core.PMatrix;
import shapes.Vertex;

public class DrawPolygonPathOLD implements PathDrawer{
	ArrayList center;
	ArrayList left;
	ArrayList right;
	float width;
	
	public DrawPolygonPathOLD(float width) {
		center = new ArrayList();
		left = new ArrayList();
		right = new ArrayList();
		this.width = width;
	}

	public void keysAdded(Path p, int amount){
		if(p.getSize() <= 2) return;
		int backStep = 0;
		if(left.size() >= 2 && right.size() >= 2){
			left.remove(left.size()-1);
			right.remove(right.size()-1);
			center.remove(center.size()-1);
			backStep = 1;
		}
		for(int i = p.getSize() - amount - backStep; i < p.getSize(); i++){
			Vector[] outline = getOutline(p, i, width);
			Vector c = p.getKeyFrameByIndex(i).getPosition();
			left.add(outline[0]);
			center.add(new Vertex(c, c.normalize()));
			right.add(outline[1]);
		}
	}

	public void keyRemoved(Path p, int i) {
		if(left.size() <= i) return;
		System.out.println("remove: " + i + " size " + left.size());
		left.remove(i);
		right.remove(i);
		center.remove(i);
	}
	
	public void keyAdded(Path p) {
		keysAdded(p, 1);
	}
	
	public void drawPath(Path path, PApplet pa) {
		pa.noStroke();
		pa.fill(15, 20, 220, 180);
		
		for(int i = 1; i < left.size(); i++){
			pa.beginShape(PApplet.TRIANGLE_STRIP);
			getLeftVertex(i-1).draw(pa);
			getCenterVertex(i-1).draw(pa);
			getCenterVertex(i).draw(pa);
			getLeftVertex(i).draw(pa);
			pa.endShape();
			
			pa.beginShape(PApplet.TRIANGLE_STRIP);
			getCenterVertex(i-1).draw(pa);
			getRightVertex(i-1).draw(pa);
			getRightVertex(i).draw(pa);
			getCenterVertex(i).draw(pa);
			pa.endShape();
		}
	}
	
	private void scaleWidth(int i, float scale){
		Vector vLeft, vCenter, vRight;
		vLeft = (Vector)left.get(i);
		vCenter = (Vector)center.get(i);
		vRight = (Vector)right.get(i);
		vLeft = vLeft.sub(vCenter).mult(scale).add(vCenter);
		vRight = vRight.sub(vCenter).mult(scale).add(vCenter);
	}
	
	private Vertex[] getOutline(Path p, int i, float width){
		Vector v0, v1, v2, v3, v4;
		
		VectorSpace tnbSpace = null;
		
		v2 = p.getKeyFrameByIndex(i).getPosition();
		if(i > 0 && i < p.getSize() - 1){ 
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
			tnbSpace = VectorSpace.newVectorSpaceFromCurve(v1, v2, v3);
		}
		/*
		else if(i == 0 && i < p.getSize() - 3) { 
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
			v4 = p.getKeyFrameByIndex(i+2).getPosition();
			tnbSpace = VectorSpace.newVectorSpaceFromCurveStart(v2, v3, v4);
		}
		else if(i > 1){ 
			v0 = p.getKeyFrameByIndex(i-2).getPosition();
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			tnbSpace = VectorSpace.newVectorSpaceFromCurveEnd(v0, v1, v2);
		}
		*/
		
		if(tnbSpace == null) return new Vertex[]{
			new Vertex(new Vector(v2)),
			new Vertex(new Vector(v2))
		};
		
		

		return new Vertex[]{
			new Vertex(v2.add(tnbSpace.getOrthoCoord(new Vector(0, width*2f, 0)))),
			new Vertex(v2.add(tnbSpace.getOrthoCoord(new Vector(0,-width*2f, 0))))
		};
	}
	private Vertex[] getOutlineOLD(Path p, int i, float width){
		Vector v1, v2, v3;
		
		v2 = p.getKeyFrameByIndex(i).getPosition();
		if(i > 0 && i < p.getSize() -1){ 
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
		}
		else if(i == 0) { 
			v1 = p.getKeyFrameByIndex(i).getPosition();
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
		}
		else { 
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = p.getKeyFrameByIndex(i).getPosition();
		}
		
		PMatrix matrix = new PMatrix(3);
		float distance = v2.absLength();
		Vector tangent = v3.sub(v1);
		Vector perp = v2.xProdut(tangent).normalize().mult(width);
		
		matrix.translate(v2.x, v2.y, v2.z); //????? minus?
		matrix.push();
		matrix.translate(perp.x, perp.y, perp.z);
		float[] left = {0, 0, 0};
		matrix.mult3(left, left);
		matrix.pop();
		perp.multMe(-1);
		matrix.translate(perp.x, perp.y, perp.z);
		float[] right = {0, 0, 0};
		matrix.mult3(right, right);
		
		PolarVector leftPolar = new Vector(left).getPolar();
		PolarVector rightPolar = new Vector(right).getPolar();
		leftPolar.d = distance;
		rightPolar.d = distance;
		Vector vLeft = leftPolar.getCartesian();
		Vector vRight = rightPolar.getCartesian();
		Vertex leftVertex = new Vertex(vLeft, vLeft.normalize());
		Vertex rightVertex = new Vertex(vRight, vRight.normalize());
		return new Vertex[]{leftVertex, rightVertex};
	}

	public Vertex getLeftVertex(int i){
		return (Vertex)left.get(i);
	}
	
	public Vertex getRightVertex(int i){
		return (Vertex)right.get(i);
	}
	
	public Vertex getCenterVertex(int i){
		return (Vertex)center.get(i);
	}

}
